package com.scut.online_black_market.controller;

import com.scut.online_black_market.dto.ChangeImage;
import com.scut.online_black_market.dto.OrderResponse;
import com.scut.online_black_market.dto.ChangeNicknameAndAddress;
import com.scut.online_black_market.dto.RegisterRequest;
import com.scut.online_black_market.dto.SuccessResponse;
import com.scut.online_black_market.entity.Personal;
import com.scut.online_black_market.exception.RegisterException;
import com.scut.online_black_market.exception.UniversalException;
import com.scut.online_black_market.exception.VerifyCodeException;
import com.scut.online_black_market.service.PersonalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户接口
 * create by yellowBroke on 2020年11月2日 22点16分
 */

@RestController
@RequestMapping(value = "/api/personal")
@Api(value = "用户接口",tags = "用户接口")
public class PersonalController {

    @Resource
    private PersonalService personalService;

    @ApiOperation("用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> register(@RequestBody RegisterRequest registerRequest) {

        try {
            personalService.register(registerRequest);
            return ResponseEntity.ok(new SuccessResponse(true, "注册成功"));
        } catch (VerifyCodeException | RegisterException e) {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }

    @ApiOperation("获取用户个人信息")
    @RequestMapping(value = "/getPersonalInfo",method = RequestMethod.GET)
    public ResponseEntity<Personal> getPersonalInfo(){
        Personal personalInfo = personalService.getPersonalInfo();
        return ResponseEntity.ok(personalInfo);
    }


    @ApiOperation("修改用户头像")
    @RequestMapping(value = "/changePersonalImage",method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> changePersonalImage(@RequestBody ChangeImage changeImage){
        try {
            personalService.changePersonalImage(changeImage);
            return ResponseEntity.ok(new SuccessResponse(true, "修改成功"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }

    @ApiOperation("查询历史订单")
    @RequestMapping(value = "/QueryHistoricalOrder", method = RequestMethod.GET)
    public ResponseEntity<List<OrderResponse>> queryOrder() {
        return ResponseEntity.ok(personalService.queryOrder());

    }

    @ApiOperation("修改用户昵称,地址和校区")
    @RequestMapping(value = "/changeNameAndAddress", method = RequestMethod.POST)
    public  ResponseEntity<SuccessResponse> changePersonalNickname(@RequestBody ChangeNicknameAndAddress changeNicknameAndAddress){
        try{
            personalService.changePersonalNicknameAndAddress(changeNicknameAndAddress);
            return ResponseEntity.ok(new SuccessResponse(true, "修改成功"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }

}
